import { login } from '../../services/user';
import { resizeImg, getUrl } from '../../utils/utils'
import { ALL, TREND, NEW, PRICE } from '../../asserts/CommodityType';
import {
  getBannerList,
  getCatListByType,
  getMenuList,
} from '../../services/home';
Page({
  data: {
    galleryPicTail: resizeImg(750, 375),
    tablist: [],
    background: ['blue', 'red', 'yellow'],
    
    topicList: [],
    topicMode: 0,

    navIndex: 1,
    navigationlist: [],
    navItemsList: [],

    zhimaQuery: {
      serviceId: '2021110100000000000089406100',
      categoryId: 'RENT_PHONE'
    },
  },
  onShow() {
    my.setNavigationBar({
      title: '首页',
      backgroundColor: '#4BA3F9'
    })
  },
  navItemClick(event) {
    this.setData({ navIndex: event.target.dataset.num });
  },
  menuClick({ target: { dataset: { key } } }) {
    my.navigateTo({
      url: `/pages/shopList/shopList?categoryId=${key}`
    })
  },
  searchgoods({ detail: { value } }) {
    my.navigateTo({url: `/pages/shopList/shopList?productName=${value}`})
  },
  onLoad(query) {
    // 页面加载
    this.getBannerList()//获取轮播图列表
    this.getTopicCat()//推荐栏目
    this.getRecommendCat()//获取推荐分类
    this.getMenuList()//获取标签列表

    if(query.bannerId){
      this.toshopUIOrg(query.bannerId, 'banner') 
    }
  },
  swipclick: function (e) {
    var id = e.target.dataset.id;

    this.toshopUIOrg(id, 'banner')
  },
  topicClick({ target: { dataset: { name } } }) {
    this.toshopUIOrg('专题', 'banner', name)
  },
  toshopUIOrg(id, type, name) {
    my.navigateTo({
      url: `/pages/shopUl/shopUl?id=${id}&type=${type}&name=${name}`,
    })
  },
  toshopUI(data) {
    console.log('data', data)
    my.navigateTo({
      url: '/pages/shopUl/shopUl?data=' + JSON.stringify(data),
    })
  },
  getBannerList() {
    getBannerList({}).then(({code, data}) => {
      code == 0 && this.setData({ background: data.map(item => {
          const { url, openType } = getUrl(item.url, item.type)
          return {
            ...item,
            url,
            openType
          }
        }) 
      })
    })
  },
  getTopicCat() {
    getCatListByType({type: '专题'}).then(({ code, data }) => {
      if (code == 0) {
        const topicList = []

        data.cats.forEach(item => {
          topicList.push({ image: item.image, name: item.name, type: item.type })
        })
        this.setData({ topicList: topicList, topicMode: topicList.length }); 
      }
    })
  },
  getRecommendCat() {
    getCatListByType({type: '首页'}).then(({ code, data }) => {
      if (code == 0) {
        const navList = []
        const navItemsList = []

        data.cats.forEach(item => {
          navList.push(item.name)
          navItemsList.push(item.items)
        })

        this.setData({ navigationlist: navList, navItemsList, navIndex: 0 }); 
      }
    })
  },
  async getMenuList() {
    const { code, data } = await getMenuList()

    if (code != 0) return false
    this.setData({ tablist: data.list.map(item => ({ tabName: item.name, tabIcon: item.icon })) })
  },
  zhimaError(error) {
    console.error('error', error)
  }
})


